package com.inossem.warehousestorage_inossem.activity.storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManagePosiBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManagePositionConfirmBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManagePositionConfirmRequestBean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.storage.StorageManagePosiRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.StorageManageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * Created by Administrator on 2017/7/16 0016.
 */

public class StorageManagePosiActivity extends BaseActivity {

    private TextView title, confirmBtn, factoryId, factoryId2, inventoryAddressId, inventoryAddressId2, warehouseId, warehouseId2, warehouseCode, warehouseCode2, warehouseDescription, warehouseDescription2;

    private EditText sendPosition, receivePosition;

    private int scanCount = 0;

    private PercentRelativeLayout secondRow, thirdRow, secondBlock;

    private View line1;

    private List<StorageManagePosiBean.BodyBean> inventoryAddressList;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_storage_manage_posi);
        initView(this);
        initData(this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        factoryId = (TextView) activity.findViewById(R.id.factoryId);
        factoryId2 = (TextView) activity.findViewById(R.id.factoryId2);
        inventoryAddressId = (TextView) activity.findViewById(R.id.inventoryAddressId);
        inventoryAddressId2 = (TextView) activity.findViewById(R.id.inventoryAddressId2);
        warehouseId = (TextView) activity.findViewById(R.id.warehouseId);
        warehouseId2 = (TextView) activity.findViewById(R.id.warehouseId2);
        warehouseCode = (TextView) activity.findViewById(R.id.warehouseCode);
        warehouseCode2 = (TextView) activity.findViewById(R.id.warehouseCode2);
        warehouseDescription = (TextView) activity.findViewById(R.id.warehouseDescription);
        warehouseDescription2 = (TextView) activity.findViewById(R.id.warehouseDescription2);
        sendPosition = (EditText) activity.findViewById(R.id.sendPosition);
        receivePosition = (EditText) activity.findViewById(R.id.receivePosition);
        confirmBtn = (TextView) activity.findViewById(R.id.confirmBtn);

        secondRow = (PercentRelativeLayout) activity.findViewById(R.id.secondRow);
        thirdRow = (PercentRelativeLayout) activity.findViewById(R.id.thirdRow);
        secondBlock = (PercentRelativeLayout) activity.findViewById(R.id.secondBlock);
        line1 = (View) activity.findViewById(R.id.line1);
    }

    private void initData(Activity activity) {
        title.setText(R.string.activity_storage_manage_posi_title);
        ActivityUtils.initBackAndFunction(activity, R.string.activity_storage_manage_title, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        setmScanListener((String scanResult, int length) -> {
            //检查发出仓库是否为空
            String position = QRCodeUtils.DecryptQRCode(scanResult);
            if (TextUtils.isEmpty(warehouseCode.getText())) {
                Utils.middleToast(activity, getResources().getString(R.string.activity_storage_manage_posi_please_choose_to_send_out_the_warehouse));
                return;
            } else {
                scanCount++;
                if (scanCount % 2 != 0) {
                    //奇数次
                    sendPosition.setText(position);
                } else {
                    //偶数次
                    receivePosition.setText(position);
                }
            }
        }, QRCodeConstant.TYPE_POSITION);

        StorageManagePosiRequest.request(activity, new GsonBeanListener<StorageManagePosiBean>() {
            @Override
            public void onSuccess(StorageManagePosiBean bean) {
                inventoryAddressList = bean.getBody();
                if (inventoryAddressList.size() == 1) {
                    StorageManagePosiBean.BodyBean inventoryAddress = inventoryAddressList.get(0);
                    inventoryAddressId.setText(inventoryAddress.getWh_id());
                    warehouseCode.setText(inventoryAddress.getWh_code());
                    warehouseDescription.setText(inventoryAddress.getWh_name());
                    inventoryAddressId2.setText(inventoryAddress.getWh_id());
                    warehouseCode2.setText(inventoryAddress.getWh_code());
                    warehouseDescription2.setText(inventoryAddress.getWh_name());
                    secondRow.setVisibility(View.VISIBLE);
                    thirdRow.setVisibility(View.VISIBLE);
                    secondBlock.setVisibility(View.VISIBLE);
                    line1.setVisibility(View.VISIBLE);
                }
            }
        });

        warehouseCode.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getStoragePositionDialog(activity, inventoryAddressList, new DialogUtils.StoragePositionListener() {
                    @Override
                    public void onSelect(Dialog dialog, String id, String code, String name, String factoryIdStr, String inventoryIdStr) {
                        factoryId.setText(factoryIdStr);
                        inventoryAddressId.setText(inventoryIdStr);
                        warehouseId.setText(id);
                        warehouseCode.setText(code);
                        warehouseDescription.setText(name);
                        factoryId2.setText(factoryIdStr);
                        inventoryAddressId2.setText(inventoryIdStr);
                        warehouseId2.setText(id);
                        warehouseCode2.setText(code);
                        warehouseDescription2.setText(name);
                        secondRow.setVisibility(View.VISIBLE);
                        thirdRow.setVisibility(View.VISIBLE);
                        secondBlock.setVisibility(View.VISIBLE);
                        line1.setVisibility(View.VISIBLE);
                    }
                }).show();

            }
        });
        confirmBtn.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String sendPosition = StorageManagePosiActivity.this.sendPosition.getText().toString();
                String receivePosition = StorageManagePosiActivity.this.receivePosition.getText().toString();
                if (TextUtils.isEmpty(sendPosition)) {
                    Utils.middleToast(activity, activity.getString(R.string.activity_storage_manage_sendPosition_no_empty_prompt));
                    StorageManagePosiActivity.this.sendPosition.requestFocusFromTouch();
                    return;
                }
                if (TextUtils.isEmpty(receivePosition)) {
                    Utils.middleToast(activity, activity.getString(R.string.activity_storage_manage_recPosition_no_empty_prompt));
                    StorageManagePosiActivity.this.receivePosition.requestFocusFromTouch();
                    return;
                }

                StorageManagePositionConfirmRequestBean params = StorageManageUtils.getStorageManagePositionConfirmRequestBean(activity, factoryId.getText().toString(), inventoryAddressId.getText().toString(), warehouseId.getText().toString(), sendPosition, receivePosition);
                StorageManagePosiRequest.submitPosi(activity, params, new GsonBeanListener<StorageManagePositionConfirmBean>() {
                    @Override
                    public void onSuccess(StorageManagePositionConfirmBean bean) {
                        if (bean.getHead().getStatus().equals(RequestConstant.STATUS_SUCCESS)) {
                            Intent intent = new Intent(activity, StorageManageResultActivity.class);
                            startActivity(intent);
                        } else {
                            if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                                Utils.middleToast(activity, bean.getHead().getMsg());
                            }
                        }
                    }

                    @Override
                    public void onFailing(StorageManagePositionConfirmBean bean) {
                        if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                            Utils.middleToast(activity, bean.getHead().getMsg());
                        }
                    }
                });
            }
        });
    }

}
